<?php
class Pages extends CI_Controller {
    public function __construct()
	{
        parent::__construct();
		$this->load->library('video/embedded_video');
		$this->load->library('HybridAuthLib');
		$this->settingdata=$this->general_model->select_val("setting","*","id = 1");
		$member = $this->session->userdata('user');
		$this->is_login=$member['logged_in'];
		$this->memberid=$member['userid'];
		//$this->is_login=1;
    }
	public function sendmail($to,$subject,$message)
	{
		//$settingdata=$this->general_model->select_val("setting","*","id = 1");
		/*$config=Array(
			"protocol"=>"smtp",
			"smtp_host"=>"ssl://smtp.gmail.com",
			"smtp_port"=>465,
			"smtp_user"=>"niranjannsahoo@gmail.com",
			"smtp_pass"=>"paikarapurdon"
		);	*/
		$this->load->library('email');
		$this->email->set_mailtype("html");
		$this->email->set_newline("\r\n");
		$this->email->from($settingdata['notification_email'],$settingdata['site_name']);
		$this->email->reply_to($settingdata['notification_email'], $settingdata['site_name']);	
		$this->email->to($to);		
		$this->email->subject($subject);		
		$this->email->message($message);
		if($this->email->send())
		{
			return 'Your email was sent';	
		}
		else
		{
			//show_error($this->email->print_debugger());
			return 'mail not send';
		}			
	}
	public function send_mail($to,$subject,$message)
	{
		$settingdata=$this->general_model->select_val("setting","*","id = 1");
		/*$config=Array(
			"protocol"=>"smtp",
			"smtp_host"=>"ssl://smtp.gmail.com",
			"smtp_port"=>465,
			"smtp_user"=>"niranjannsahoo@gmail.com",
			"smtp_pass"=>"paikarapurdon"
		);
		$this->load->library('email',$config);*/
		$this->load->library('email');
		$this->email->set_mailtype("html");
		$this->email->set_newline("\r\n");
		$this->email->from($settingdata['notification_email'],$settingdata['site_name']);
		$this->email->reply_to($settingdata['notification_email'], $settingdata['site_name']);	
		$this->email->to($to);		
		$this->email->subject($subject);		
		$this->email->message($message);
		$this->email->send();
	}
    public function index()
	{
		$data['page'] =$this->general_model->select_val("pages","*","slug = '".(string) $this->uri->segment(1)."'");
		$method = '_' . $data['page']['template'];
    	if (method_exists($this, $method)) {
    		$this->$method($data['page']);
    	}
    	else {
			$data['page'] =$this->general_model->select_val("pages","*","is_home = 1");
			$method = '_' . $data['page']['template'];
			if (method_exists($this, $method)) {
    		$this->$method($data['page']);
			}
    		//log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
    		//show_error('Could not load template ' . $method);
    	}
		//$this->template->content->view('cms/content', $data);
		$this->template->publish();
    }
	private function _page($data)
	{
		$data['page']=$data;
		//$data['page'] =$this->general_model->select_val("pages","*","slug = '".(string) $this->uri->segment(1)."'");
		$this->load->library('Shortcode');
		$data['content']=$this->shortcode->run($data['page']['content']);
		//echo $data['content'];
		$this->template->set_template('template/page_template');
		
    	$this->template->content->view('front/content', $data);
    }
	private function _homepage($data)
	{
		$data['page']=$data;
		if($this->is_login)
		{
			try
			{
				$data['connected_list']  = $this->hybridauthlib->getConnectedProviders(); 
				
			}
			catch( Exception $e )
			{
				echo "Ooophs, we got an error: " . $e->getMessage();

				echo " Error code: " . $e->getCode();

				echo "<br /><br />Please try again.";

				echo "<hr /><h3>Trace</h3> <pre>" . $e->getTraceAsString() . "</pre>"; 
			}
			
			$data['zapmember']=$this->general_model->select_val("zap_member","*","userid = ".$this->memberid);
			$data['frontmenu']=1;
			$data['userid']=$this->memberid;
			$myfriends=$this->general_model->select("zap_friends","friend_id","userid = ".$this->memberid);
			$wherein=array();
			foreach($myfriends as $friends)
			{
				$wherein[]=$friends['friend_id'];
			}
			$wherein[]=$this->memberid;
			
			$data['comments']=$this->general_model->selectmult_jointable("zap_post","zap_member","*,zap_post.id,zap_post.date_added","zap_post.userid=zap_member.userid","zap_post.userid",$wherein,"zap_post.date_added","DESC");
			$i=0;
			foreach($data['comments'] as $comment)
			{
				$data['comments'][$i]['comment']=$this->general_model->selectmult_jointableord("zap_member","zap_postcomment","*","zap_member.userid=zap_postcomment.userid","zap_postcomment.postid = ".$comment['id'],"zap_postcomment.comment_date","DESC");
				$i++;
			}
			/*echo "<pre>";
			print_r($data['comments']);*/
			$this->template->content->view('front/dashboard',$data);
		}
		else
		{
			$this->load->library('Shortcode');
			$data['content']=$this->shortcode->run($data['page']['content']);
			$this->template->content->view('front/homecontent', $data);
		}
    }
	private function _blog($data)
	{
		$data['page']=$data;
		//$this->template->set_template('template/cms_blogtemplate');
		$count = $this->general_model->count_row("post","status = 'published'");
		// Set up pagination
		$perpage = 4;
		if ($count > $perpage) {
			$this->load->library('pagination');
			$config['base_url'] = site_url($this->uri->segment(1) . '/');
			$config['total_rows'] = $count;
			$config['per_page'] = $perpage;
			$config['uri_segment'] = 2;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$offset = $this->uri->segment(2);
		}
		else {
			$data['pagination'] = '';
			$offset = 0;
		}
		$data['blogs'] = $this->general_model->select_rows("post","*","date_added","desc",$perpage,$offset);
		/*echo "<pre>";
		print_r($data['blogs']);*/
    	$this->template->content->view('cms/blogcontent', $data);
    }
	private function _contact($data)
	{
		$data['page']=$data;
		$this->form_validation->set_rules('name', 'Name', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
		$this->form_validation->set_rules('message', 'Message', 'required');		
			//$member=$this->session->userdata('user');
			if ($this->form_validation->run() == TRUE)
			{
				$m_message="Welcome to LuLu Law. <br />Hi ".$this->input->post('name').",<br> We glad you contact us. One of our representative will get back to your shortly.";
				$a_message=$this->input->post('name'). " had sent a request to contact him regarding some issue.";
				//send email
				$this->send_mail($this->input->post('email'),'LuLu Law Contact Request',$m_message);
				$this->send_mail($this->settingdata['notification_email'],'LuLu Law Contact Request',$a_message);
				$this->session->set_flashdata('message', '<p class="success">Thank You for contacting LuLu Law. One of our representative will contact you shortly.</p>');
				redirect(BASE_URL .$data['slug']);
			}
    	$this->template->content->view('cms/contactus', $data);
    }
	private function _landingpage($data)
	{
		$data['page']=$data;
		$this->template->set_template('template/cms_landingtemplate');
		$this->load->library('Shortcode');
		$data['content']=$this->shortcode->run($data['page']['content']);
    	$this->template->content->view('cms/landingpage', $data);
    }
	public function validateForm()
	{
		$registrationrules=array(
			'email' => array(
			'field' => 'email', 
			'label' => 'Email', 
			'rules' => 'trim|required|valid_email|is_unique[users.email]'
			)
		);
		$this->form_validation->set_rules($registrationrules);
		$this->form_validation->set_error_delimiters('<label class="error">', '</label>');
		if ($this->form_validation->run() == TRUE)
		{
			return true;
    	}
		else
		{
      		return false;
    	}
	}
}